package util

import (
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
)

type Response struct {
	Code    int         `json:"code"`
	Message interface{} `json:"message,omitempty"`
	Data    interface{} `json:"data,omitempty"`
}

// 成功响应
func SuccessResponse(ctx *gin.Context, data interface{}, message ...interface{}) {
	var msg string
	if len(message) > 0 {
		msg = fmt.Sprint(message[0])
	}
	ctx.JSON(http.StatusOK, Response{
		Code:    200,
		Message: msg,
		Data:    data,
	})
}

// 失败响应
func FailResponse(ctx *gin.Context, err error, codes ...int) {
	code := 500
	if len(codes) > 0 {
		code = codes[0]
	}
	ctx.JSON(http.StatusOK, Response{
		Code:    code,
		Message: err.Error(),
	})

}
